package com.huaxindata.im.receiver;

import android.app.Notification;
import android.app.NotificationManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;
import android.util.Log;

import com.huaxindata.im.R;
import com.huaxindata.im.http.RetrofitClient;
import com.huaxindata.im.model.contact.UserMessage;
import com.huaxindata.im.model.entity.ChatMessage;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * @author songxf
 * @date 2017/12/26
 * @Desc 接受消息通知广播
 * Copyright HUAXIN DATA. All rights reserved.
 */
public class HxShowNotificationReceiver extends BroadcastReceiver {
    private static final String TAG = "HxShowNotificationRecei";
    private static int mNotificationNum;

    @Override
    public void onReceive(Context context, Intent intent) {
        ChatMessage message = intent.getParcelableExtra("RECEIVER_MESSAGE");
        Log.d(TAG, "onReceive:------消息推送 ");
        setUserInfo(message, context);

    }

    /**
     * 获取用户信息(可优化:后期新建用户表，可直接在用户表里面查看相关信息)
     *
     * @param message
     * @param context
     */
    public void setUserInfo(final ChatMessage message, final Context context) {
        Call<UserMessage> call = RetrofitClient.getHttpApi().getUserById(message.sender);
        call.enqueue(new Callback<UserMessage>() {
            @Override
            public void onResponse(Call<UserMessage> call, Response<UserMessage> response) {
                if (response.isSuccessful()) {
                    UserMessage messageBean = response.body();
                    if (response.body().getState() == 1) {
                        //获取NotificationManager实例
                        NotificationManager notifyManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
                        //实例化NotificationCompat.Builde并设置相关属性
                        NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
                                //设置小图标
                                .setSmallIcon(R.mipmap.icon_logo)
                                //设置通知标题
                                .setContentTitle(messageBean.getNickName())
                                //设置通知内容
                                .setContentText(message.getSimpleString());
                        //设置通知时间，默认为系统发出通知的时间，通常不用设置
                        //.setWhen(System.currentTimeMillis());


                        //设置 Notification 的 flags = FLAG_NO_CLEAR
                        //FLAG_AUTO_CANCEL 表示该通知能被状态栏的清除按钮给清除掉
                        //等价于 builder.setAutoCancel(true);
                        Notification notification = builder.build();
                        notification.flags |= Notification.FLAG_AUTO_CANCEL;
                        notifyManager.notify(mNotificationNum++, notification);
                    }
                }
            }

            @Override
            public void onFailure(Call<UserMessage> call, Throwable t) {

            }
        });
    }
}
